package cn.wikiflyer.beikang.act.user.setting;

import java.lang.reflect.Type;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wikiflyer.beikang.utils.task.GetVerifyCodeTask;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.utils.WKCountDownUtils;
import cn.wk.libs4a.utils.WKUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 找回密码
 * 
 * @author qunlee_mr
 * 
 */
public class UserSettingFindPwdAct extends BaseActivity {

	@ViewInject(id = R.id.find_tel)
	private EditText tel;

	@ViewInject(id = R.id.find_code)
	private EditText code;

	@ViewInject(id = R.id.find_new_pwd)
	private EditText new_pwd;
	@ViewInject(id = R.id.find_new_pwd2)
	private EditText new_pwd2;

	@ViewInject(id = R.id.find_get_codebtn, click = "onClick")
	private Button get_codebtn;

	@ViewInject(id = R.id.find_commit, click = "onClick")
	private Button commit;

	private WKCountDownUtils cdUtils = null;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_find_pwd);
	}

	@Override
	public void init() {
	}

	@Override
	public void setData() {
		cdUtils = new WKCountDownUtils(get_codebtn, "获取验证码", 600, 1);
	}

	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.find_get_codebtn:
			getCodeAction();
			break;
		case R.id.find_commit:
			commitPwdAction();
			break;

		default:
			break;
		}
	}

	String telText;
	String codeText;
	String newPwdText;

	private boolean verification(boolean newPwd) {
		telText = tel.getText().toString().trim();
		if (TextUtils.isEmpty(tel.getText())) {
			Toast.makeText(ctx, "请输入您的手机号码", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (!WKUtils.isTel(telText)) {
			Toast.makeText(ctx, "手机号码格式不正确", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (newPwd) {
			codeText = code.getText().toString().trim();
			newPwdText = new_pwd.getText().toString();
			if (TextUtils.isEmpty(code.getText())) {
				Toast.makeText(ctx, "请输入验证码", Toast.LENGTH_SHORT).show();
				return false;
			}

			if (!WKUtils.isPassword(newPwdText)) {
				Toast.makeText(ctx, "密码格式不正确", Toast.LENGTH_SHORT).show();
				return false;
			}
			if (TextUtils.isEmpty(new_pwd.getText())) {
				Toast.makeText(ctx, "请输入新密码", Toast.LENGTH_SHORT).show();
				return false;
			}
		}

		return true;
	}

	private void getCodeAction() {
		if (!verification(false))
			return;
		// 获取验证码
		cdUtils.start();
		new GetVerifyCodeTask(this, tel.getText().toString());
	}

	private void commitPwdAction() {
		if (!verification(true))
			return;

		// 提交新密码
		new updatePwdTask(ctx);
	}

	/**********************************************************************
	 * 数据接口
	 ***********************************************************************/

	/**
	 * 提交新密码
	 * 
	 * { result: "success", message: "成功", data: { } }
	 */
	class updatePwdTask extends WKAsyncTaskPro {
		public updatePwdTask(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... params) {
			return app().net.update_psw(telText, codeText, newPwdText);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			String msg = base.info();

			if ("success".equals(base.info())) {
				Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
				UserSettingFindPwdAct.this.finish();
			} else {
				Toast.makeText(ctx, "失败" + msg, Toast.LENGTH_SHORT).show();
				UserSettingFindPwdAct.this.finish();
			}
		}

	}

}
